package com.lry.basic.netty.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/9/18
 */
public class Server {
    public static void main(String[] args) throws IOException {

        ExecutorService executor = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),
                100,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(100));

        ServerSocket server = new ServerSocket(9999);
        System.out.println("server start");
        Socket socket = null;
        try {
            while(true){
                socket = server.accept();
                executor.execute(new SocketTask(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            server.close();
            socket.close();
        }
    }

    static class SocketTask implements Runnable{
        private Socket socket;
        public SocketTask(Socket socket){
            this.socket = socket;
        }
        @Override
        public void run() {
            System.out.println("connect success,客户端ip:"+socket.getInetAddress()+" port:"+socket.getPort());
        }
    }
}
